<?php
include './include/common.inc.php';
include 'category.class.php';
include './include/model.class.php';
include './include/workflow.class.php';
include './include/menu.class.php';
require '../include/form.class.php';
$siteid = get_current_site();
$site = new site();
$site_info = $site->get_one($siteid);
$category_db = new category();
$model_db = new model();
$workflow_db = new workflow();
$menu_db = new menu();
$action = isset( $action ) ? $action : 'manage';
$forward = get_forward( 'category.php' );

$params = array();
array_push( $params, "status = 99" );
array_push( $params, "parentid = ''" );
$order = " ORDER BY listorder DESC, inputtime DESC";
$model_list = $model_db->get_all_model();

if( $current_user === false )
{
	if( isset( $edufepm ) && $edufepm == 'iframe' )
	{
		$response->alert( '请您登陆后再访问' );
		$response->redirect( $base_dir . 'admin/login.php' );
		$response->execute();
	}
	else
	{
		showmessage( '请您登陆后再访问', $base_dir . 'admin/login.php' );
	}
}

switch( $action )
{
    case 'add':
        $have_perm = check_perms( 'C002' );
        if( isset( $_POST['catname'] ) )
		{
            if( $have_perm === false )
			{
				$response->alert( '您没有权限进行此操作！' );
				$response->execute();
			}
			if( empty( $catname ) || strlen( $catname ) > 200 )
			{
				$response->text_alert( 'error','catname','栏目名称不能为空且长度不能大于200个字符' );
			}
            $siteid = get_current_site();
            $check_name = $category_db->check_catname( $parentid, $catname, $siteid);
            if( !empty($check_name) )
            {
                $response->text_alert( 'error','catname','栏目名称已存在' );
            }
            if( empty( $catdir ) || strlen( $catdir ) > 200 )
			{
				$response->text_alert( 'error','catdir','栏目目录不能为空且长度不能大于200个字符' );
			}
            $check_dir = $category_db->check_dir( $parentid, $catdir, $siteid );
            if( !empty($check_dir) )
            {
                $response->text_alert( 'error','catdir','栏目目录已存在' );
            }
            if( empty( $c_temp ) )
			{
				$response->text_alert( 'error','c_temp','栏目模板不能为空，请选择' );
			}
            if( empty( $l_temp ) )
			{
				$response->text_alert( 'error','l_temp','列表模板不能为空，请选择' );
			}
            if( empty( $s_temp ) )
			{
				$response->text_alert( 'error','s_temp','内容模板不能为空，请选择' );
			}
			$response->real_execute();
			$params = array(
                'parentid'    =>  $parentid,
				'catname'	  =>  $catname,
				'catcode'	  =>  $catcode,
                'catdir'      =>  $catdir,
                'siteid'	  =>  $siteid,
                'image'       =>  $image,
                'description' =>  $description,
                'modelid'     =>  $modelid,
                'workflowid'  =>  $workflowid,   //添加工作流
                'linkurl'     =>  isset( $islink ) ? $linkurl : '',
                'islink'      =>  isset( $islink ) ? $islink : 0,
                'isnavigation'=>  isset( $isnavigation ) ? $isnavigation : 0,
                'isshow'      =>  isset( $isshow ) ? $isshow : 0,
                'showother'   =>  isset( $showother ) ? $showother : 0,
                'showinother' =>  isset( $showinother ) ? $showinother : 0,
                'clew'        =>  isset( $isclew ) ? $isciew : 0,
                //'ismenu'      =>  $ismenu,     //去除是否加入菜单项
                'status'      =>  99,
                'username'    =>  $_SESSION['current_user']['username'],
                'inputtime'   =>  time(),
                'c_temp'      =>  $c_temp,
                'l_temp'      =>  $l_temp,
                's_temp'      =>  $s_temp,
                'cat_page'    =>  intval($cat_page)==0 ? 20 : intval($cat_page),
			);
            $params['setting'] = serialize( $params );//插入数据序列
            $res = $category_db->add_category( $params );
            $catid = $db->uuid();//获取最新一条数据插入ID
            $menu_parent_id = 0;//菜单父ID赋初值为0
            if( !empty( $parentid ) && strlen( $parentid ) > 0 )
            {
                $arrparentid = $category_db->get_arrparentid( $parentid ) . ',' . $parentid;//获取所有父ID
                $parentdir = $category_db->get_parentdir( $parentid ) . $CATEGORY[$parentid]['catdir'] . '/';
                //菜单相关设置
                $keyid = 'category_' . $parentid;
                $menu_single = $menu_db->get_keyid( $keyid );
                $menu_parent_id = $menu_single['id'];

            }
            else
            {
                $arrparentid = '0';
                $parentdir = '';
                $menu_parent_id = $site_info['menunews'];//菜单的父ID
            }
            $url = $parentdir . $catdir . '/';
            $edit_list = array( 'arrparentid'=>$arrparentid, 'parentdir'=>$parentdir, 'url'=>$url );
            //修改所有父CHILD字段start
            $arrparentid_array = explode(",", $arrparentid);
            foreach( $arrparentid_array as $cat_id )
            {
                $category_db->edit_category( array( 'child' => 1 ), $cat_id );
            }
            //修改所有父CHILD字段end
            $category_db->edit_category( $edit_list, $catid );//插入父信息
            //将在菜单信息中加入栏目的菜单
            $data = array(
                'name'	      =>  $catname,
                'parentid'    =>  $menu_parent_id,
                'siteid'	  =>  $siteid,
                'url'         =>  "admin/content.php?catid=$catid",
                'status'      =>  99,
                'keyid'       =>  'category_' . $catid,
            );
            $menu_db->add( $data );
            if( $parentid )
            {
                $menu_db->update_isfolder_status( $menu_parent_id );
            }

            if( intval( $res ) > 0 )
			{
				$response->alert( '添加成功' );
                cache_all_category();
				$response->redirect( urldecode( $forward ) );
			}
			else
			{
				$response->alert( '添加失败' );
				$response->script( 'self.location = self.location' );
			}
			$response->execute();
		}
		else
		{
            if( $have_perm === false )
			{
				showmessage('您没有权限进行此操作！');
			}
			
			$df_workflow = isset($site_info['workflowid']) ? $site_info['workflowid'] : 0;
            $category_list = $category_db->get_all_category($siteid);
            $workflow_list = $workflow_db->workflow_list();//获取工作流数据
			include template( 'admin', 'category_add' );
		}
        break;
    case 'add_list':
        $have_perm = check_perms( 'C002' );
        if( isset( $_POST['catname'] ) )
		{
            if( $have_perm === false )
			{
				$response->alert( '您没有权限进行此操作！' );
				$response->execute();
			}
            $siteid = get_current_site();
            
            
            if( empty( $c_temp ) )
			{
				$response->text_alert( 'error','c_temp','栏目模板不能为空，请选择' );
			}
            if( empty( $l_temp ) )
			{
				$response->text_alert( 'error','l_temp','列表模板不能为空，请选择' );
			}
            if( empty( $s_temp ) )
			{
				$response->text_alert( 'error','s_temp','内容模板不能为空，请选择' );
			}
            $catnames = explode('|',$catname);
            $catdirs = explode('|',$catdir);
            if(sizeof($catnames) != sizeof($catdirs))
            {
                $response->text_alert( 'error','catdir','栏目名称与目录不匹配' );
            }
            foreach ($catnames as $k=>$cn)
            {
                $cd = $catdirs[$k];
                $check_name = $category_db->check_catname( $parentid, $cn, $siteid);
                if( !empty($check_name) )
                {
                    $response->text_alert( 'error','catname','栏目名称已存在' );
                    break;
                }
                $check_dir = $category_db->check_dir( $parentid, $cd, $siteid );
                if( !empty($check_dir) )
                {
                    $response->text_alert( 'error','catdir','栏目目录已存在' );
                    break;
                }
            }
            
			$response->real_execute();
             foreach ($catnames as $k=>$cn)
             {
                $cd = $catdirs[$k];
                $params = array(
                    'parentid'    =>  $parentid,
                    'catname'	  =>  $cn,
                    'catcode'	  =>  $catcode,
                    'catdir'      =>  $cd,
                    'siteid'	  =>  $siteid,
                    'image'       =>  $image,
                    'description' =>  $description,
                    'modelid'     =>  $modelid,
                    'workflowid'  =>  $workflowid,   //添加工作流
                    'linkurl'     =>  isset( $islink ) ? $linkurl : '',
                    'islink'      =>  isset( $islink ) ? $islink : 0,
                    'isnavigation'=>  isset( $isnavigation ) ? $isnavigation : 0,
                    'isshow'      =>  isset( $isshow ) ? $isshow : 0,
                    'showother'   =>  isset( $showother ) ? $showother : 0,
                    'showinother' =>  isset( $showinother ) ? $showinother : 0,
                    'clew'        =>  isset( $isclew ) ? $isciew : 0,
                    //'ismenu'      =>  $ismenu,     //去除是否加入菜单项
                    'status'      =>  99,
                    'username'    =>  $_SESSION['current_user']['username'],
                    'inputtime'   =>  time(),
                    'c_temp'      =>  $c_temp,
                    'l_temp'      =>  $l_temp,
                    's_temp'      =>  $s_temp,
                    'cat_page'    =>  intval($cat_page)==0 ? 20 : intval($cat_page),
                );
                $params['setting'] = serialize( $params );//插入数据序列
                $res = $category_db->add_category( $params );
                $catid = $db->uuid();//获取最新一条数据插入ID
                $menu_parent_id = 0;//菜单父ID赋初值为0
                if( !empty( $parentid ) && strlen( $parentid ) > 0 )
                {
                    $arrparentid = $category_db->get_arrparentid( $parentid ) . ',' . $parentid;//获取所有父ID
                    $parentdir = $category_db->get_parentdir( $parentid ) . $CATEGORY[$parentid]['catdir'] . '/';
                    //菜单相关设置
                    $keyid = 'category_' . $parentid;
                    $menu_single = $menu_db->get_keyid( $keyid );
                    $menu_parent_id = $menu_single['id'];

                }
                else
                {
                    $arrparentid = '0';
                    $parentdir = '';
                    $menu_parent_id = $site_info['menunews'];//菜单的父ID
                }
                $url = $parentdir . $cd . '/';
                $edit_list = array( 'arrparentid'=>$arrparentid, 'parentdir'=>$parentdir, 'url'=>$url );
                //修改所有父CHILD字段start
                $arrparentid_array = explode(",", $arrparentid);
                foreach( $arrparentid_array as $cat_id )
                {
                    $category_db->edit_category( array( 'child' => 1 ), $cat_id );
                }
                //修改所有父CHILD字段end
                $category_db->edit_category( $edit_list, $catid );//插入父信息
                //将在菜单信息中加入栏目的菜单
                $data = array(
                    'name'	      =>  $cn,
                    'parentid'    =>  $menu_parent_id,
                    'siteid'	  =>  $siteid,
                    'url'         =>  "admin/content.php?catid=$catid",
                    'status'      =>  99,
                    'keyid'       =>  'category_' . $catid,
                );
                $menu_db->add( $data );
                if( $parentid )
                {
                    $menu_db->update_isfolder_status( $menu_parent_id );
                }
            }
            if( intval( $res ) > 0 )
			{
				$response->alert( '添加成功' );
                cache_all_category();
				$response->redirect( urldecode( $forward ) );
			}
			else
			{
				$response->alert( '添加失败' );
				$response->script( 'self.location = self.location' );
			}
			$response->execute();
		}
		else
		{
            if( $have_perm === false )
			{
				showmessage('您没有权限进行此操作！');
			}
			
			$df_workflow = isset($site_info['workflowid']) ? $site_info['workflowid'] : 0;
            $category_list = $category_db->get_all_category($siteid);
            $workflow_list = $workflow_db->workflow_list();//获取工作流数据
			include template( 'admin', 'category_add_list' );
		}
        break;
    case 'upd':
        $have_perm = check_perms( 'C003' );
        if( isset( $_POST['catname'] ) )
		{
            if( $have_perm === false )
			{
				$response->alert( '您没有权限进行此操作！' );
				$response->execute();
			}
            //查询已存在栏目信息
			$info = $category_db->get_category( $catid, $siteid );
            if( empty( $catname ) || strlen( $catname ) > 200 )
			{
				$response->text_alert( 'error','catname','栏目名称不能为空且长度不能大于200个字符' );
			}
            //判断父ID或栏目名称是否改变
            if( $info['catname'] != $catname || $info['parentid'] != $parentid )
            {
                $check_name = $category_db->check_catname( $parentid, $catname );//改变验证栏目名称是否重复
            }
            if( !empty($check_name) )
            {
                $response->text_alert( 'error','catname','栏目名称已存在' );
            }
            if( empty( $catdir ) || strlen( $catdir ) > 100 )
			{
				$response->text_alert( 'error','catdir','栏目目录不能为空且长度不能大于100个字符' );
			}
            //判断父ID或栏目目录是否改变
            if( $info['catdir'] != $catdir || $info['parentid'] != $parentid )
            {
                $check_dir = $category_db->check_dir( $parentid, $catdir, $siteid );//改变验证栏目目录是否重复
            }
            if( !empty($check_dir) )
            {
                $response->text_alert( 'error','catdir','栏目目录已存在' );
            }
            if( empty( $c_temp ) )
			{
				$response->text_alert( 'error','c_temp','栏目模板不能为空，请选择' );
			}
            if( empty( $l_temp ) )
			{
				$response->text_alert( 'error','l_temp','列表模板不能为空，请选择' );
			}
            if( empty( $s_temp ) )
			{
				$response->text_alert( 'error','s_temp','内容模板不能为空，请选择' );
			}
			$response->real_execute();
            //修改2011-06-10
            if( !empty( $parentid ) && strlen( $parentid ) > 0 )
            {
                $arrparentid = $category_db->get_arrparentid( $parentid ) . ',' . $parentid;//获取所有父ID
                $parentdir = $category_db->get_parentdir( $parentid ) . $CATEGORY[$parentid]['catdir'] . '/';

            }
            else
            {
                $arrparentid = '0';
                $parentdir = '';
            }
            //end
            $url = $parentdir . $catdir . '/';
            $params = array(
                'parentid'    =>  $parentid,
				'catname'	  =>  $catname,
				'catcode'	  =>  $catcode,
                'catdir'      =>  $catdir,
                'image'       =>  $image,
                'description' =>  $description,
                'modelid'     =>  $modelid,
                'workflowid'  =>  $workflowid,//添加工作流
                'linkurl'     =>  isset( $islink ) ? $linkurl : '',
                'islink'      =>  isset( $islink ) ? $islink : 0,
                'isnavigation'=>  isset( $isnavigation ) ? $isnavigation : 0,
                'isshow'      =>  isset( $isshow ) ? $isshow : 0,
                'showother'   =>  isset( $showother ) ? $showother : 0,
                'showinother' =>  isset( $showinother ) ? $showinother : 0,
                'clew'        =>  isset( $isclew ) ? $isciew : 0,
                //'ismenu'      =>  $ismenu,  //去除是否加入菜单项
                'arrparentid' =>  $arrparentid,
                'parentdir'   =>  $parentdir,
                'url'         =>  $url,
                'username'    =>  $_SESSION['current_user']['username'],
                'updatetime'  =>  time(),
                'c_temp'      =>  $c_temp,
                'l_temp'      =>  $l_temp,
                's_temp'      =>  $s_temp,
                'cat_page'    =>  intval($cat_page)==0 ? 20 : intval($cat_page),
			);
            $params['setting'] = serialize( $params );//数据序列化
            //修改所有父CHILD字段start
            $arrparentid_array = explode(",", $arrparentid);
            foreach( $arrparentid_array as $cat_id )
            {
                $category_db->edit_category( array( 'child' => 1 ), $cat_id );
            }
            //修改所有父CHILD字段end
			$res = $category_db->edit_category( $params, $catid );

            //修改菜单信息
            $menu_parent_keyid = 'category_' . $parentid;
            $menu_parent_single = $menu_db->get_keyid( $menu_parent_keyid );
            $menu_parent_id = (!isset($menu_parent_single['id']) || empty($menu_parent_single['id'])) ? $site_info['menunews'] : $menu_parent_single['id'];
            $menu_keyid = 'category_' . $catid;
            $menu_single = $menu_db->get_keyid( $menu_keyid );
            $menuid = $menu_single['id'];
            $data = array(
                'name'	      =>  $catname,
                'parentid'    =>  $menu_parent_id,
                'url'         =>  "admin/content.php?catid=$catid",
                'status'      =>  99,
                'keyid'       =>  'category_' . $catid,
            );
            $menu_db->update( $menuid, $data );
            if( $info['parentid'] != $parentid )
            {
                if( $menu_parent_id ) $menu_db->update_isfolder_status( $menu_parent_id );
            }

			if( intval( $res ) > 0 )
			{
				$response->alert( '修改成功' );
                cache_all_category();
				$response->redirect( urldecode( $forward ) );
			}
			else
			{
				$response->alert( '修改失败' );
				$response->script( 'self.location = self.location' );
			}
			$response->execute();
		}
		else
		{
            if( $have_perm === false )
			{
				showmessage('您没有权限进行此操作！');
			}
			$info = $category_db->get_category( $catid );
			if( !isset( $info['catid'] ) )
			{
				showmessage( '要编辑的栏目不存在' );
			}
            $category_list = $category_db->get_all_category($siteid);
            $workflow_list = $workflow_db->workflow_list();//获取工作流数据
			include template( 'admin', 'category_upd' );
		}
        break;

    case 'del':
        $have_perm = check_perms( 'C004' );
        if( $have_perm === false )
        {
            $response->alert( '您没有权限进行此操作！' );
            $response->execute();
        }
		$info = $category_db->get_category( $catid, $siteid );
		if( !isset( $info['catid'] ) )
		{
			$response->alert( '要删除的栏目不存在' );
			$response->execute();
		}
		$res = $category_db->del_category( $catid );
		if( $res == false )
		{
            cache_all_category();
			$response->alert( '删除失败' );
		}
		else
		{
            $menu_keyid = 'category_' . $catid;
            $menu_single = $menu_db->get_keyid( $menu_keyid );
            $menu_db->delete( $menu_single['id'] );
			$response->alert( '删除成功' );
			$response->script( 'self.location=self.location' );
		}
		$response->execute();
        break;

    case 'del_all':
        $have_perm = check_perms( 'C004' );
        if( $have_perm === false )
        {
            $response->alert( '您没有权限进行此操作！' );
            $response->execute();
        }
        if( isset( $_POST['cid'] ) )
		{
			foreach ( $cid as $id )
			{
				$category_db->del_category( $id );
                $menu_keyid = 'category_' . $id;
                $menu_single = $menu_db->get_keyid( $menu_keyid );
                $menu_db->delete( $menu_single['id'] );
			}
			$response->alert( '删除成功！' );
			$response->script( 'self.location=self.location' );
			$response->execute();
		}
		else
		{
			$response->alert( '请选择要删除的栏目' );
			$response->execute();
		}
        cache_all_category();
        break;

    case 'listorder':
        $have_perm = check_perms( 'C005' );
        if( $have_perm === false )
        {
            $response->alert( '您没有权限进行此操作！' );
            $response->execute();
        }
        $result = $category_db->listorder( $listorder );
		if( $result )
		{
            $response->alert( '修改成功' );
            $response->redirect( urldecode( $forward ) );
		}
		else
		{
			$response->alert( '修改失败' );
		}
        $response->execute();
        break;

    case 'child':
        $have_perm = check_perms( 'C006' );
        if( $have_perm === false )
        {
            $response->alert( '您没有权限进行此操作！' );
            $response->execute();
        }
        $params = array();
        isset($catid) ? $catid : 0;
        array_push( $params, "status = ".STATUS_NORMAL );
        array_push( $params, "parentid = '$catid'" );
        array_push( $params, "siteid = '$siteid'" );
        $category_list = $category_db->get_list_no( $params, $order );
        $make_forward = make_forward();
		include template( 'admin', 'category_child' );
        break;

    case 'manage':

    default:
        $have_perm = check_perms( 'C001' );
        if( $have_perm === false )
        {
            showmessage( '您没有权限进行此操作！');
        }
        $page = isset( $page ) ? intval( $page ) : 1;
		$page = max( $page, 1 );
        $pagesize = 30;
        $canview = ( $current_user['usertype'] == USER_TYPE_SUPER ) ? true : get_view_resource( 'category' );
        $params[] = ' siteid="'.$siteid.'"';
		$category_list = $category_db->get_list( $params, $order, $canview, $page, $pagesize );
        $category_pages = $category_db->pages;
        $forward = make_forward();
		include template( 'admin', 'category_manage' );
        break;
}

?>
